package com.lms.logistics_management_system.controller;

import cn.hutool.core.util.StrUtil;
import com.lms.logistics_management_system.common.Constants;
import com.lms.logistics_management_system.common.Result;
import com.lms.logistics_management_system.controller.dto.DepartmentRankDto;
import com.lms.logistics_management_system.entity.Doctor;
import com.lms.logistics_management_system.entity.QueueCondition;
import com.lms.logistics_management_system.mapper.DoctorMapper;
import com.lms.logistics_management_system.mapper.QueueConditionMapper;
import com.lms.logistics_management_system.service.DoctorService;
import com.lms.logistics_management_system.service.QueueConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/scan")
public class ScanController {

    @Autowired
    private QueueConditionService queueConditionService;
    @Autowired
    private QueueConditionMapper queueConditionMapper;

    // 签到
    @PostMapping("/signIn")
    public Result signIn(@RequestParam Integer reservationId){
        return Result.success(queueConditionService.addQueueCondition(reservationId));
    }

    // 排队
    @PostMapping("/queue")
    public Result queue(@RequestBody QueueCondition queueCondition){
        return Result.success(queueConditionService.updateQueueCondition(queueCondition));
    }

    // 排队总人数
    @GetMapping("/queueCount")
    public Result queueCount(@RequestParam Integer departmentId, @RequestParam Integer roomId){
        Map<String, Object> res = new HashMap<>();
        res.put("queueCount",queueConditionMapper.queueCount(departmentId, roomId));
        return Result.success(res);
    }

    // 排在前面的人数
    @GetMapping("/queueAheadCount")
    public Result queueAheadCount(@RequestParam Integer reservationId){
        Map<String, Object> res = new HashMap<>();
        res.put("queueAheadCount", queueConditionMapper.queueAheadCount(reservationId));
        return Result.success(res);
    }

    // 请求下一人，返回下一人的订单号，为null表示没有下一人
    @PostMapping("/queueNext")
    public Result queueNext(@RequestParam Integer departmentId, @RequestParam Integer roomId){
        Map<String, Object> res = new HashMap<>();
        res.put("queueNext", queueConditionService.queueNext(departmentId, roomId));
        return Result.success(res);
    }

    // 获取所有科的相关信息并按照升序排序
    @GetMapping("/allDepartmentRank")
    public Result allDepartmentRank(@RequestParam Integer reservationId){
        Map<String, Object> res = new HashMap<>();
        res.put("queueNext", queueConditionService.allDepartmentRank(reservationId));
        return Result.success(res);
    }

    @GetMapping("/export")
    public void export(@RequestParam Integer reservationId, HttpServletResponse response) throws Exception {
        queueConditionService.export(reservationId, response);
    }

}
